
let dataList = [
    {
        "姓名": "李支山",
        "OA账号": "lizhishan",
        "联系电话": "18602030197"
    },
    {
        "姓名": "余佩玲",
        "OA账号": "yupeiling",
        "联系电话": "18602031828"
    },
    {
        "姓名": "杨晨",
        "OA账号": "yangchen23",
        "联系电话": "18620011859"
    },
    {
        "姓名": "李勇军",
        "OA账号": "liyj286",
        "联系电话": "18602030955"
    },
    {
        "姓名": "陈丹群",
        "OA账号": "chendq32",
        "联系电话": "18602032015"
    },
    {
        "姓名": "周洪波",
        "OA账号": "zhouhb22",
        "联系电话": "18602034569"
    },
    {
        "姓名": "陈洋",
        "OA账号": "cheny1034",
        "联系电话": "18620018287"
    },
    {
        "姓名": "范荣辉",
        "OA账号": "fanronghui",
        "联系电话": "18602030278"
    },
    {
        "姓名": "陈国豪",
        "OA账号": "chengh83",
        "联系电话": "18602030560"
    },
    {
        "姓名": "丁先诚",
        "OA账号": "dingxc",
        "联系电话": "18602031545"
    },
    {
        "姓名": "何小利",
        "OA账号": "hexl67",
        "联系电话": "18602032998"
    },
    {
        "姓名": "汪亮",
        "OA账号": "liangwang",
        "联系电话": "18602030078"
    },
    {
        "姓名": "邹志锋",
        "OA账号": "zouzf3",
        "联系电话": "18620012322"
    },
    {
        "姓名": "周艳",
        "OA账号": "zhouy81",
        "联系电话": "18602031195"
    },
    {
        "姓名": "郑泽彬",
        "OA账号": "zhengzb21",
        "联系电话": "18620010447"
    },
    {
        "姓名": "王薇",
        "OA账号": "wangwei816",
        "联系电话": "18602030537"
    },
    {
        "姓名": "冯永雄",
        "OA账号": "fengyx",
        "联系电话": "18602031881"
    },
    {
        "姓名": "赵江",
        "OA账号": "zhaojiang9",
        "联系电话": "18620011482"
    },
    {
        "姓名": "王宇翔",
        "OA账号": "wangyx2458",
        "联系电话": "18620010521"
    },
    // {
    //     "姓名": "司徒智敏",
    //     "OA账号": "gd-sitzm",
    //     "联系电话": "18620012248"
    // },
    {
        "姓名": "杨薇",
        "OA账号": "yangwei202",
        "联系电话": "18602030964"
    },
    {
        "姓名": "夏隽依",
        "OA账号": "xiajy8",
        "联系电话": "18602030685"
    },
    {
        "姓名": "徐远钿",
        "OA账号": "xuyd",
        "联系电话": "18602039955"
    },
    {
        "姓名": "贾占一",
        "OA账号": "jiazy23",
        "联系电话": "18602030024"
    },
    {
        "姓名": "罗文东",
        "OA账号": "luowd5",
        "联系电话": "18689390866"
    },
    {
        "姓名": "平怀逸",
        "OA账号": "pinghy6",
        "联系电话": "18602030507"
    },
    {
        "姓名": "陈勋",
        "OA账号": "chenx116",
        "联系电话": "18602031753"
    },
    {
        "姓名": "朱友芬",
        "OA账号": "zhuyf22",
        "联系电话": "18602031670"
    },
    {
        "姓名": "李国方",
        "OA账号": "ligf95",
        "联系电话": "18689398939"
    },
    {
        "姓名": "李春华",
        "OA账号": "lich271",
        "联系电话": "18602030407"
    },
    {
        "姓名": "王巍",
        "OA账号": "wangwei599",
        "联系电话": "18602030189"
    },
    {
        "姓名": "邝伟辉",
        "OA账号": "kuangwh",
        "联系电话": "18602031448"
    },
    {
        "姓名": "温志坚",
        "OA账号": "wenzj12",
        "联系电话": "18602032854"
    },
    {
        "姓名": "黎美婵",
        "OA账号": "limc39",
        "联系电话": "18602033847"
    },
    {
        "姓名": "肖寰",
        "OA账号": "xiaoh53",
        "联系电话": "18602034110"
    },
    {
        "姓名": "齐宇锋",
        "OA账号": "yufengqi",
        "联系电话": "18602031812"
    },
    {
        "姓名": "于昊哲",
        "OA账号": "yuhz39",
        "联系电话": "15602860017"
    },
    {
        "姓名": "杨柠旭",
        "OA账号": "yangnx5",
        "联系电话": "18602031943"
    },
    {
        "姓名": "慕长行",
        "OA账号": "muzx3",
        "联系电话": "15602869971"
    },
    {
        "姓名": "李春材",
        "OA账号": "licc93",
        "联系电话": "18602030283"
    },
    {
        "姓名": "李广生",
        "OA账号": "ligs70",
        "联系电话": "15602860859"
    },
    {
        "姓名": "陈立英",
        "OA账号": "chenly51",
        "联系电话": "15602860018"
    },
    {
        "姓名": "赵维丽",
        "OA账号": "zhaowl36",
        "联系电话": "15602860097"
    },
    {
        "姓名": "陈勇成",
        "OA账号": "gd-chenyc73",
        "联系电话": "15602860701"
    },
    {
        "姓名": "彭龙",
        "OA账号": "pengl118",
        "联系电话": "15602862225"
    },
    {
        "姓名": "郭连娣",
        "OA账号": "guold9",
        "联系电话": "15602861171"
    },
    {
        "姓名": "覃汉机",
        "OA账号": "qinhj11",
        "联系电话": "15602861806"
    },
    {
        "姓名": "利小军",
        "OA账号": "lixj268",
        "联系电话": "15602869836"
    },
    {
        "姓名": "尼涵郁",
        "OA账号": "nihy3",
        "联系电话": "15602861227"
    },
    {
        "姓名": "李慧",
        "OA账号": "lih256",
        "联系电话": "15602861180"
    },
    {
        "姓名": "张春明",
        "OA账号": "gd-zhangcm",
        "联系电话": "15602866182"
    },
    {
        "姓名": "潘茂华",
        "OA账号": "panmh3",
        "联系电话": "15602860618"
    },
    {
        "姓名": "陈健冰",
        "OA账号": "jianbingchen",
        "联系电话": "15602860673"
    },
    {
        "姓名": "王贤标",
        "OA账号": "wangxb96",
        "联系电话": "15602862228"
    },
    {
        "姓名": "张向鹏",
        "OA账号": "zhangxp1",
        "联系电话": "15602869511"
    },
    {
        "姓名": "林彬彬",
        "OA账号": "linbb5",
        "联系电话": "15602860898"
    },
    {
        "姓名": "勾勒",
        "OA账号": "goule1",
        "联系电话": "18602030971"
    },
    {
        "姓名": "陈利婷",
        "OA账号": "gd-chenlt2",
        "联系电话": "15602860554"
    },
    {
        "姓名": "陈洋",
        "OA账号": "gd-cheny55",
        "联系电话": "15602862085"
    },
    {
        "姓名": "林龙斌",
        "OA账号": "gd-linlb7",
        "联系电话": "15602860681"
    },
    {
        "姓名": "张文忠",
        "OA账号": "gd-zhangwz",
        "联系电话": "15602860757"
    },
    {
        "姓名": "冯铃娟",
        "OA账号": "gd-fenglj",
        "联系电话": "15602860761"
    },
    {
        "姓名": "王东娟",
        "OA账号": "gd-wangdj3",
        "联系电话": "15602860371"
    },
    {
        "姓名": "洪鑫",
        "OA账号": "gd-hongx5",
        "联系电话": "15602867598"
    },
    {
        "姓名": "田宇",
        "OA账号": "gd-tianyu",
        "联系电话": "15602867750"
    },
    {
        "姓名": "张文",
        "OA账号": "gd-zhangw8",
        "联系电话": "15602865625"
    },
    {
        "姓名": "梁家铭",
        "OA账号": "gd-liangjm3",
        "联系电话": "15602869152"
    },
    {
        "姓名": "杨伟业",
        "OA账号": "yangweiye",
        "联系电话": "15602860913"
    },
    {
        "姓名": "李淑英",
        "OA账号": "gd-lisy11",
        "联系电话": "15602861750"
    },
    {
        "姓名": "李子良",
        "OA账号": "lizl182",
        "联系电话": "15602860523"
    },
    {
        "姓名": "何家健",
        "OA账号": "gd-hejj5",
        "联系电话": "15602869272"
    },
    {
        "姓名": "黄乾永",
        "OA账号": "gd-huangqy3",
        "联系电话": "15602867573"
    },
    {
        "姓名": "赵章广",
        "OA账号": "gd-zhaozg",
        "联系电话": "15602861290"
    },
    {
        "姓名": "张敏峰",
        "OA账号": "gd-zhangmf",
        "联系电话": "15602860189"
    }
]


const queryList = (realname, userId) => {
    var myHeaders = new Headers();
    myHeaders.append("X-Access-Token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2OTcwODgyMTcsInVzZXJuYW1lIjoiQk9OQ19aWUcifQ.6I3mzgQrRKrj2T8dRtoULeafriq0DHN-U1wbtBpto_U");
    myHeaders.append("tenant_id", "0");
    myHeaders.append("User-Agent", "Apifox/1.0.0 (https://apifox.com)");

    var requestOptions = {
        method: 'GET',
        headers: myHeaders,
        redirect: 'follow'
    };

    return fetch("http://10.177.77.86:3000/wgyybonc/sys/user/list?_t=1697079404&orgId=root&username=**&realname=" + realname + "&column=createTime&order=desc&field=id,,username,realname,phone,orgCodeTxt,userAreaDesc,status_dictText,action&pageNo=1&pageSize=10", requestOptions)
        .then(response => response.json())
        .then(res => {
            const item = res.result.records.find(it => it.id = userId)
            return item
        })
        .catch(error => console.log('error', error));
}
const queryRole = (userId) => {
    return fetch("http://10.177.77.86:3000/wgyybonc/sys/user/queryUserRole?_t=1697080499&userid=" + userId, {
        "headers": {
            "accept": "application/json, text/plain, */*",
            "accept-language": "zh-CN",
            "tenant_id": "0",
            "x-access-token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2OTcwODgyMTcsInVzZXJuYW1lIjoiQk9OQ19aWUcifQ.6I3mzgQrRKrj2T8dRtoULeafriq0DHN-U1wbtBpto_U"
        },
        "referrer": "http://10.177.77.86:3000/wyy/isystem/user",
        "referrerPolicy": "strict-origin-when-cross-origin",
        "body": null,
        "method": "GET",
        "mode": "cors",
        "credentials": "omit"
    }).then(res => res.json()).then(res => {
        const selectedroles = Array.from(new Set([...(res.result || []), '1381887158494244866'])).join(',')
        console.log({ selectedroles })
        return selectedroles
    })
}
const queryDepartment = (userId) => {
    return fetch("http://10.177.77.86:3000/wgyybonc/sys/user/userDepartList?_t=1697080499&userId=" + userId, {
        "headers": {
            "accept": "application/json, text/plain, */*",
            "accept-language": "zh-CN",
            "tenant_id": "0",
            "x-access-token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2OTcwODgyMTcsInVzZXJuYW1lIjoiQk9OQ19aWUcifQ.6I3mzgQrRKrj2T8dRtoULeafriq0DHN-U1wbtBpto_U"
        },
        "referrer": "http://10.177.77.86:3000/wyy/isystem/user",
        "referrerPolicy": "strict-origin-when-cross-origin",
        "body": null,
        "method": "GET",
        "mode": "cors",
        "credentials": "omit"
    }).then(res => res.json().then(res => {
        console.log(res)
        const selecteddeparts = res.result.map(it => it.value).join(',')
        console.log({ selecteddeparts })
        return selecteddeparts
    }))
}
const querySysUserDatapermission = (userId) => {
    return fetch("http://10.177.77.86:3000/wgyybonc/sys/sysUserDatapermission/queryByUserId?_t=1697080499&userId=" + userId, {
        "headers": {
            "accept": "application/json, text/plain, */*",
            "accept-language": "zh-CN",
            "tenant_id": "0",
            "x-access-token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2OTcwODgyMTcsInVzZXJuYW1lIjoiQk9OQ19aWUcifQ.6I3mzgQrRKrj2T8dRtoULeafriq0DHN-U1wbtBpto_U"
        },
        "referrer": "http://10.177.77.86:3000/wyy/isystem/user",
        "referrerPolicy": "strict-origin-when-cross-origin",
        "body": null,
        "method": "GET",
        "mode": "cors",
        "credentials": "omit"
    }).then(res => res.json()).then(res => {
        console.log(res)
        const selecteddatapermission = res.result.map(it => it.value).join(',')
        console.log({ selecteddatapermission })
        return selecteddatapermission

    })
}
const querySelecteddbimsatapermission = (userId) => fetch("http://10.177.77.86:3000/wgyybonc/sys/sysUserDatapermission/queryBimsByUserId?_t=1697081355&userId=" + userId, {
    "headers": {
        "accept": "application/json, text/plain, */*",
        "accept-language": "zh-CN",
        "tenant_id": "0",
        "x-access-token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2OTcwODgyMTcsInVzZXJuYW1lIjoiQk9OQ19aWUcifQ.6I3mzgQrRKrj2T8dRtoULeafriq0DHN-U1wbtBpto_U"
    },
    "referrer": "http://10.177.77.86:3000/wyy/isystem/user",
    "referrerPolicy": "strict-origin-when-cross-origin",
    "body": null,
    "method": "GET",
    "mode": "cors",
    "credentials": "omit"
}).then(res => res.json()).then(res => {
    console.log(res)
    const selecteddbimsatapermission = res.result.map(it => it.value).join(',')
    console.log({ selecteddbimsatapermission })
    return selecteddbimsatapermission
})
const edit = (body) => fetch("http://10.177.77.86:3000/wgyybonc/sys/user/edit", {
    "headers": {
        "accept": "application/json, text/plain, */*",
        "accept-language": "zh-CN",
        "content-type": "application/json;charset=UTF-8",
        "tenant_id": "0",
        "x-access-token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2OTcwODgyMTcsInVzZXJuYW1lIjoiQk9OQ19aWUcifQ.6I3mzgQrRKrj2T8dRtoULeafriq0DHN-U1wbtBpto_U"
    },
    "referrer": "http://10.177.77.86:3000/wyy/isystem/user",
    "referrerPolicy": "strict-origin-when-cross-origin",
    "body": JSON.stringify(body),
    // "{\"birthday\":\"\",\"relTenantIds\":\"NaN\",\"sex_dictText\":\"男\",\"activitiSync\":1,\"userAreaDesc\":\"广东省分公司本部\",\"userIdentity\":\"1\",\"status_dictText\":\"正常\",\"delFlag\":0,\"appsalt\":\"NULL\",\"dyFlag\":\"N\",\"workNo\":\"lizhishan\",\"post\":\"\",\"updateBy\":\"BONC_ZYG\",\"orgCode\":\"root/00440004500\",\"app_password\":null,\"id\":\"lizhishan\",\"appRole\":null,\"email\":\"lizhishan@chinaunicom.cn\",\"datapermissionMap\":[{\"lvl\":\"3\",\"orgCode\":\"root\",\"title\":\"广东省分公司\",\"value\":\"root\",\"key\":\"root\"}],\"post_dictText\":null,\"clientId\":\"NULL\",\"sex\":1,\"dyFlag_dictText\":\"\",\"departIds_dictText\":null,\"telephone\":\"NULL\",\"updateTime\":\"2023-10-12 11:33:35\",\"departIds\":\"\",\"avatar\":\"NULL\",\"realname\":\"李支山\",\"createBy\":\"dfgx_anyujia\",\"phone\":\"18602030197\",\"createTime\":\"2023-09-14 15:00:00\",\"orgCodeTxt\":\"广东省分公司市场部\",\"userRole\":null,\"username\":\"lizhishan\",\"status\":1,\"userAreaId\":\"004400\",\"selectedroles\":\"1381887158494244866\",\"selecteddeparts\":\"00440004500\",\"selecteddatapermission\":\"root\",\"selecteddbimsatapermission\":\"\"}",
    "method": "POST",
    "mode": "cors",
    "credentials": "omit"
}).then(res => res.json().then(console.log))

// dataList = dataList.slice(0, 22)
const main = async () => {

    while (dataList.length) {
        const item = dataList.pop()
        console.warn('====>剩余', dataList.length)
        // "姓名": "李支山",
        // "OA账号": "lizhishan",
        const userId = item['OA账号']
        const userItem = await queryList(item['姓名'], userId)
        if (userItem) {
            const attrs = {
                "selectedroles": await queryRole(userId),
                "selecteddeparts": await queryDepartment(userId),
                "selecteddatapermission": await querySysUserDatapermission(userId),
                "selecteddbimsatapermission": await querySelecteddbimsatapermission()
            }
            const res = await edit({ ...userItem, ...attrs })
            console.log(JSON.stringify(item), 'add结果', res)

        } else {
            console.warn(JSON.stringify(item), '查询失败')
        }
    }
}
main()